package com.lovo.project2.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lovo.project2.bean.RoleBean;
import com.lovo.project2.bean.UserBean;
import com.lovo.project2.mapper.IUserBeanMapper;
import com.lovo.project2.service.IUserBeanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserBeanServiceImpl extends ServiceImpl<IUserBeanMapper, UserBean> implements IUserBeanService {
    @Autowired
    IUserBeanMapper iUserBeanMapper;

    @Override
    public UserBean selectUserBean(UserBean userBean) {
        return iUserBeanMapper.queryAllByNameOrderByPassWordUserBean(userBean.getUserName(), userBean.getPassWord());
    }

    @Override
    public boolean updateUserBean(UserBean userBean) {
        baseMapper.updateById(userBean);
        return true;
    }

    @Override
    public void addUserBean(UserBean userBean) {
        //添加用户表
        baseMapper.inserUser(userBean);

        List<Integer> list = new ArrayList<>();
        for (RoleBean r : userBean.getRoles()) {
            list.add(r.getRoleId());
        }

        //给用户角色赋值
        baseMapper.addUserRole(userBean.getUserId(), list);

    }

    @Transactional
    @Override
    public void removeUserBean(int id) {
        //删除用户表和角色的关系表
        baseMapper.deleteByUserIdUserBeanRole(id);
//删除用户表的数据
        baseMapper.deleteById(id);


    }

    @Override
    public boolean updataUserBeanPwd(String newPwd, int userId) {
UserBean userBean=new UserBean();
userBean.setUserId(userId);
userBean.setPassWord(newPwd);
baseMapper.updateById(userBean);
        return false;
    }
//调用这个方法 查看密码是否正确
    @Override
    public boolean queryPwd(int userId, String oldPwd) {

        UserBean userBean1=baseMapper.queryAByIdByPassWordUserBean(userId,oldPwd);
if (userBean1!=null){
    return true;
}
        return false;
    }
}
